package com.example.mydemo2.ui.login;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydemo2.IndexActivity;
import com.example.mydemo2.MyApplication;
import com.example.mydemo2.R;
import com.example.mydemo2.RegisterActivity;
import com.example.mydemo2.User;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity {

    private LoginViewModel loginViewModel;
    Context context =  LoginActivity.this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText etUsername = findViewById(R.id.tv_username);
        EditText etPassword = findViewById(R.id.tv_password);
        Button btnLogin = findViewById(R.id.login);
        TextView register  = findViewById(R.id.textView);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //给intent   跳转环境、跳转的页面
                Intent intent = new Intent(context, IndexActivity.class);
                String username = etUsername.getText().toString();
                //String username1 = "admin";
                String pwd = etPassword.getText().toString();

                User user = new User();
                user.setUsername(username);
                user.setPwd(pwd);
                System.out.println("用户登录："+user.getUsername());

                if(TextUtils.isEmpty(user.getUsername()) || TextUtils.isEmpty(user.getPwd()))
                {
                    Toast.makeText(LoginActivity.this,
                            "用户名或者密码不为空！", Toast.LENGTH_SHORT).show();
                }else {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FormBody.Builder params = new FormBody.Builder();
                                params.add("userName",user.getUsername());
                                params.add("userCode",user.getPwd());

                                MyApplication url = (MyApplication) getApplication();
                                url.getUrl();

                                OkHttpClient client = new OkHttpClient();//创建http客服端
                                Request request = new Request.Builder() //请求ip 数据
                                        .url(url.getUrl()+"login")
                                        .post(params.build())
                                        .build();
                                Response response = client.newCall(request).execute();//执行请求
                                //System.out.println("________________response:"+response);

                                String responseData = response.body().string();
                                System.out.println(responseData);
                                JSONObject jsonObject = new JSONObject(responseData); //创建json对象 传入数据
                                //传入的参数是数据字段名称
                                String status=jsonObject.getString("status");
                                System.out.println("status:"+status);

                                switch (status){
                                    case "200":
                                        intent.putExtra("user", user);
                                        startActivity(intent);
                                        finish();
                                        Looper.prepare();
                                        Toast.makeText(LoginActivity.this,
                                                "登录成功", Toast.LENGTH_SHORT).show();
                                        Looper.loop();

                                    case "404":
                                        Looper.prepare();
                                        Toast.makeText(LoginActivity.this,
                                            "用户名或者密码不正确！请重新输入", Toast.LENGTH_SHORT).show();
                                        Looper.loop();
                                }

                            }catch (Exception e){
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginActivity.this,"服务器异常，请联系管理员",Toast.LENGTH_SHORT).show();

                                    }
                                });

                            }

                        }
                    }).start();



                }

            }

        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, RegisterActivity.class);
                startActivity(intent);


            }
        });


    }
}